﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AssociationCorpLogic
{
    public class Association
    {
        //Variables:
        private bool uncovered = false;
        private List<Association> associations = new List<Association>();
        private Character[] word;

        //Attributes:
        public bool Uncovered { get { return this.uncovered; } }
        public List<Association> Associations { get { return this.associations; } }
        public string Word
        {
            get
            {
                string returnString = "";

                //fill returnstring with characters, until the first undiscovered character
                bool notUncoveredCharacterFound = false;
                for (int i = 0; i < this.word.Length; i++)
                {
                    //if a bar is found, fill rest of returnstring with bars
                    if (!this.word[i].Uncovered)
                    {
                        notUncoveredCharacterFound = true;
                    }
                    
                    //fill returnstring with characters or bars
                    if (!notUncoveredCharacterFound)
                    {
                        returnString += this.word[i].Self.ToString();
                    }
                    else
                    {
                        returnString += "-";
                    }
                }
                return returnString;
            }
        }

        //Constructors:
        public Association(string word)
        {
            this.word = new Character[word.Length];
            for (int i = 0; i < word.Length; i++)
            {
                this.word[i] = new Character(word.ToLower().Substring(i,1));
            }
        }
        public Association(string word, List<Association> associations)
            : this(word)
        {
            this.associations = associations;
        }

        //Events:
        //Methods:
        public void AddAssociation(Association association)
        {
            if (!this.associations.Contains(association))
            {
                this.associations.Add(association);
            }
        }
        public void RemoveAssociation(Association association)
        {
            if (this.associations.Contains(association))
            {
                this.associations.Remove(association);
            }
        }
        public bool TryUnlock(string tryWord)
        {
            string tempTryWord = tryWord.Trim().ToLower();
            
            //error handling
            if (tempTryWord.Length == 0) { return false; }

            //check for partial match
            if (tempTryWord.Length < this.word.Length) { tempTryWord = tempTryWord.PadRight(tempTryWord.Length + (this.word.Length - tempTryWord.Length), '%'); }
            if (tempTryWord.Length > this.word.Length) { tempTryWord = tempTryWord.Substring(0, this.word.Length); }
            //loop through the association word and uncover characters if they are correct
            bool wholeWordUncovered = true;
            for (int i = 0; i < this.word.Length; i++)
            {
                bool uncover = this.word[i].Uncover((char)tempTryWord[i]);
                if (!uncover) {
                    wholeWordUncovered = false;
                    break;
                }
            }

            if (wholeWordUncovered)
            {
                this.uncovered = true;
                return true;
            }
            //TODO: generate failed unlocking event?
            //no character or some characters uncovered
            return false;
        }
    }
}
